package com.lucy.wx.miniapp.service.impl;

import cn.hutool.core.lang.Validator;
import com.lucy.common.constant.HttpStatus;
import com.lucy.common.domain.PageResultDTO;
import com.lucy.common.exception.BusinessException;
import com.lucy.wx.miniapp.dao.MiniAppGrantDAO;
import com.lucy.wx.miniapp.entity.MiniAppGrant;
import com.lucy.wx.miniapp.service.MiniAppGrantService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * @author kindear
 * 小程序授权服务实现
 */
@Slf4j
@Service
public class MiniAppGrantServiceImpl implements MiniAppGrantService {
    @Autowired
    MiniAppGrantDAO dao;

    @Override
    public MiniAppGrant getOneUnwrap(String s) {
        Optional<MiniAppGrant> appOp = dao.findById(s);
        if (!appOp.isPresent()){
            throw new BusinessException(HttpStatus.NOT_FOUND);
        }
        return appOp.get();
    }



    @Override
    public PageResultDTO<MiniAppGrant> getAll(Pageable pageable, Example<MiniAppGrant> example) {
        Page<MiniAppGrant> page;

        if (Validator.isEmpty(example)) {
            page = dao.findAll(pageable);
        } else {
            page = dao.findAll(example, pageable);
        }
        /*
          获取分页主体内容
         */
        List<MiniAppGrant> list = page.getContent();
        PageResultDTO<MiniAppGrant> pageResultDTO = new PageResultDTO<>();


        //封装分页请求结果
        pageResultDTO.setContent(list);
        pageResultDTO.setSize(page.getSize());
        pageResultDTO.setTotalPages(page.getTotalPages());
        pageResultDTO.setNumber(page.getNumber());
        pageResultDTO.setTotalElements(page.getTotalElements());
        return pageResultDTO;
    }

    @Override
    public List<MiniAppGrant> getExample(Example<MiniAppGrant> example) {
        return dao.findAll(example);
    }

    @Override
    public void deleteOne(String id) {
        dao.deleteById(id);
    }

    @Override
    public Optional<MiniAppGrant> getOne(String id) {
        return dao.findById(id);
    }

    @Override
    public MiniAppGrant saveOne(MiniAppGrant o) {
        if (Validator.isNotEmpty(o.getId())) {
            throw new BusinessException(HttpStatus.BAD_REQUEST, "新建DTO不可携带ID");
        }
        return dao.save(o);
    }

    @Override
    public MiniAppGrant updateOne(MiniAppGrant o) {
        if (Validator.isEmpty(o.getId())) {
            throw new BusinessException(HttpStatus.BAD_REQUEST, "更新DTO必须携带ID");
        }
        return dao.save(o);
    }

    @Override
    public MiniAppGrant getApp(String appId) {
        Optional<MiniAppGrant> grantOp = dao.findByAppId(appId);
        if (!grantOp.isPresent()){
            throw new BusinessException(HttpStatus.NOT_FOUND);
        }
        return grantOp.get();
    }
}
